#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

ll qmi(ll a, ll b)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		x ^= x << 13;
		x ^= x >> 7;
		x ^= x << 17;
		return x; 
	}
	size_t operator () (uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); // 时间戳
		return splitmix64(x + FIXED_RANDOM);
	}
};

    bool st[10010];
    int primes[10010];
    int cnt;
    void fun(int n)
    {
        for(int i = 2; i < n; i++)
        {
            if(!st[i]){
                primes[cnt++] = i;
            }
            for(int j = 0; i * primes[j] < n; j++)
            {
                st[i * primes[j]] = true;
                if(i % primes[j] == 0) break;
            }
        }
    }
    int ans = INT_MAX;
    string s;
    void dfs(int n, int m, int sum, int fa) {
        if(sum >= ans) return;
        if(n == m) {
            ans = sum;
            return;
        }
        s = to_string(n);
        cout << s << endl;
        for(int i = 1, j = s.size() - 1; i <= n && j >= 0; i *= 10, j--) {
            if(s[j] < '9' && st[n + i] && n + i != fa) dfs(n + i, m, sum + (n + i), n);
            if(s[j] > '0' && st[n - i] && n - i != fa) dfs(n - i, m, sum + (n - i), n);


        }
    }

    int main() {
        fun(10002);
        int n, m;
        cin >> n >>m;
        dfs(n, m, 0, -1);
        cout << ans << endl;
        return 0;
    }